package client

import (
	"context"
	"fmt"
	"github.com/go-redis/redis"
	"log"
	"time"
)

var (
	Rdb *redis.Client
)

func initRedis() {
	//配置Redis连接参数
	password := EnvConf.RedisConf.PassWord //密码
	host := EnvConf.RedisConf.Host         //数据库地址，可以是Ip或者域名
	port := EnvConf.RedisConf.Port         //数据库端口
	Dbname := EnvConf.RedisConf.Db         //数据库名
	poolSize := EnvConf.RedisConf.poolSize //连接池大小

	Rdb = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", host, port),
		Password: password, // no password set
		DB:       Dbname,   // use default DB
		PoolSize: poolSize, // 连接池大小
	})
	_, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	_, err := Rdb.Ping().Result()
	if err != nil {
		log.Fatalf("open redis error %v", err)
	}
}
